package com.kadron.service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.kadron.common.KConst;
import com.kadron.common.KInitParams;
import com.kadron.common.KUtil;
import com.kadron.entity.Discount;
import com.kadron.entity.User;
import com.kadron.enumeration.UserStatus;
import com.kadron.enumeration.UserType;
import com.kadron.item.KItemModel;

public class UserDiscountService {
	
	private static final Log log = LogFactory.getLog(UserDiscountService.class);
	
	public static int getDiscount(User user) {
		int discount = 0;
		if (!UserStatus.REGISTERED.equals(user.getUserStatus()) && !UserStatus.CONFIRMED.equals(user.getUserStatus())) {
			return 0;
		}
		UserType type = user.getUserType();
		
		Date moment = KUtil.getMoment();
		Date regDate = user.getRegDate();
		Date userTypeDate = user.getStatusUpdate(); //Date when become certain user type(SILVER, GOLD, PLATINUM)
		
		Calendar registered = Calendar.getInstance();
		registered.setTime(regDate);
		int yearOfRegistration = registered.get(Calendar.YEAR);
		
		boolean leapYear = KUtil.isLeapYear(yearOfRegistration);
		
		long year = (leapYear ? KConst.leapYearMilisec : KConst.yearMilisec);
		
		long timeBetween = moment.getTime() - regDate.getTime();
		long userTypeTimeBetween = moment.getTime() - userTypeDate.getTime();
		
		switch (type) {
		case STANDARD_FIST_BUY:
		case STANDARD_10:
			if (timeBetween < KConst.monthMilisec) {				
				discount = 10;
			}
			
			break;
			
		case SILVER:
			if (userTypeTimeBetween < year) {				
				discount = KInitParams.SILVER_DISCOUNT;
			}
			
			break;
			
		case GOLD:
			if (userTypeTimeBetween < year) {				
				discount = KInitParams.GOLD_DISCOUNT;
			}

			break;
			
		case PLATINUM:
			if (userTypeTimeBetween < year) {				
				discount = KInitParams.PLATINUM_DISCOUNT;
			}
			
			break;
			
		default:
			if (user.getUserStatus().equals(UserStatus.REGISTERED)) {				
				if (timeBetween < KConst.tenDaysMilisec) {
					discount = 10; //10% in 10- days
				} else {
					discount = 5; //5% in 10+ days
				}
			}
			break;
		}
		return discount;
	}
	
	public static KItemModel doPrice(KItemModel model, int userDiscount, BigDecimal price) {
		Discount discount = model.getDiscount();
		if (discount == null || !discount.isActive()) {
			if (log.isDebugEnabled()) {
				log.debug("Discount is null or not active");
			}
			return model;
		}
		int percentInt = discount.getPercent();
		BigDecimal percent = new BigDecimal(percentInt);
		BigDecimal onePercent = new BigDecimal(0.01);
		
		BigDecimal finalPrice = null;
		
		BigDecimal discountPrice = price.multiply(onePercent.subtract(percent.multiply(percent)));
		discountPrice = discountPrice.setScale(2, RoundingMode.HALF_UP);
		model.setDiscountPrice(discountPrice);
		finalPrice = new BigDecimal(discountPrice.doubleValue());
		
		if (userDiscount > 0) {
			BigDecimal userPercent = new BigDecimal(userDiscount);
			finalPrice = discountPrice.multiply(onePercent.subtract(discountPrice.multiply(userPercent)));
		}
		finalPrice = finalPrice.setScale(2, RoundingMode.HALF_UP);
		model.setYourPrice(finalPrice);
		return model;
	}
	
	public static BigDecimal getDiscountPrice(Discount discount, int userDiscount, BigDecimal price) {
		if (discount == null || !discount.isActive()) {
			if (log.isDebugEnabled()) {
				log.debug("Discount is null or not active");
			}
			return price;
		}
		int percentInt = discount.getPercent();
		BigDecimal percent = new BigDecimal(percentInt);
		BigDecimal one = new BigDecimal(KConst._ONE);
		BigDecimal hundred = new BigDecimal(KConst._HUNDERED);
		
		BigDecimal discountPrice = price.multiply(one.subtract(percent.divide(hundred)));
		discountPrice = discountPrice.setScale(2, RoundingMode.HALF_UP);
		return discountPrice;
	}
	
	public static BigDecimal getYourPrice(int userDiscount, BigDecimal price) {
		if (userDiscount <= 0) {
			if (log.isDebugEnabled()) {
				log.debug("Discount is null or not active");
			}
			return price;
		}
		BigDecimal one = new BigDecimal(KConst._ONE);
		BigDecimal hundred = new BigDecimal(KConst._HUNDERED);
		
		BigDecimal userPercent = new BigDecimal(userDiscount);
		
		BigDecimal finalPrice = price.multiply(one.subtract(userPercent.divide(hundred)));
		finalPrice = finalPrice.setScale(2, RoundingMode.HALF_UP);
		return finalPrice;
	}
	
}
